<?php
/**
 * @copyright: ©2024 永州 微聚 商 网络科技有限公司
 * @link: https://www.weiJuyunke.com
 */
namespace app\api\controller\consume;
use app\admin\model\Application;
use app\admin\model\StoreJoinActivity;
use app\common\controller\AdminController;
use think\App;

/**
 * @ControllerAnnotation(title="会员管理")
 */
class Store extends AdminController
{
    
    use \app\admin\traits\Curd;

    /**
     * 字段排序
     *
     * @var array
     */
    protected $sort = [
        'create_time' => 'desc'
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        
        $this->model = new \app\admin\model\Store();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        $request = $this->request;
        $param = $request->param();
        $page = input("page");
        $limit = input("limit");
        $where[] = ['store.is_delete','=',0];

        if ($this->request->platform_id >0 ){
            $where[] = ['store.platform_id','=',$this->request->platform_id];
        }
        if (isset( $param['store_name']) &&   $param['store_name']){
            $where[] = ['store_name','like',"%{$param['store_name']}%"];
        }
        if (isset( $param['nickname']) &&   $param['nickname']){
            $where[] = ['userInfo.nickname','like',"%{$param['nickname']}%"];
        }
        if (isset( $param['telphone']) &&   $param['telphone'] ){
            $where[] = ['store.telphone','=',$param['telphone']];
        }

        if (isset( $param['platform_name']) &&   $param['platform_name'] ){
            $where[] = ['platformInfo.name','=',$param['platform_name']];
        }
        $count = $this->model->withJoin([
            'platformInfo',
            'userInfo'
            ], 'LEFT')
            ->where($where)
            ->count();

        $list = $this->model->withJoin([

            'platformInfo',
            'userInfo',
        ], 'LEFT')
            ->where($where)
            ->page($page, $limit)
            ->order($this->sort)
            ->select();



        
        return msg([
            'list' => $list,
            'count' => $count
        ], 0, "success");
    }

    public function delete(){
        $request = $this->request;
        $param = $request->param();
        $this->model->whereIn('id', $param['id'])->update(['is_delete' => 1]);

        return msg([

        ], 0, "删除成功");
    }

    public function  modify(){
        $request = $this->request;
        $param = $request->param();

        $this->model->where(['id' =>$param['id'] ])->update([$param['field'] => $param['value']]);
        $storeInfo =  $this->model->where(['id' =>$param['id'] ])->find();
        $userModel = new \app\admin\model\User();
        $userInfo = $userModel->where(['id' => $storeInfo['uid']])->find();
        $platform  = new \app\admin\model\Platform();
        $platformInfo =  $platform->where(['id' => $storeInfo['platform_id']])->find();
        $withdraw = new \app\admin\model\template\Store();
        $templateModel   = new \app\admin\model\Template();
        $templateInfo = $templateModel->where(['name'=>'store',
            'platform_id'=> $storeInfo['platform_id']])->find();
        if ($templateInfo){
            if ($userInfo['openid']){
                $sendData = [
                    'template_id' => $templateInfo['template_id'],
                    'appid' => $platformInfo['xcx_appid'],
                    'appsecret' => $platformInfo['xcx_appsecret'],
                    'openid' => $userInfo['openid'],
                    'store_name' => $storeInfo['store_name'],
                ];
                if ($param['value'] == 1){
                    $sendData['result'] = '审核通过';
                }else if ($param['value'] == 2){
                    $sendData['result'] = '审核驳回';
                }
                $withdraw->send($sendData);
            }
        }


        return  msg([], 0, "操作成功") ;
    }

    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();


           try {

                $save = $row->save($post);

                $platform  = new \app\admin\model\Platform();
                $platformInfo =  $platform->where(['id' => $row['platform_id']])->find();


                if ($post['is_application'] != 1){
                    $userModel = new \app\admin\model\User();
                    $userInfo  = $userModel->where(['id' => $row['uid']])->find();
                    $templateModel   = new \app\admin\model\Template();
                    $templateInfo = $templateModel->where(['name'=>'ticket_review',
                        'platform_id'=> $row['platform_id']])->find();
                    $withdraw = new \app\admin\model\template\TicketReview();

                    if ($templateInfo){
                        if ($userInfo['openid']){
                            $sendData = [
                                'template_id' => $templateInfo['template_id'],
                                'appid' => $platformInfo['xcx_appid'],
                                'appsecret' => $platformInfo['xcx_appsecret'],
                                'openid' => $userInfo['openid'],
                                'desc' => $post['is_application'] == 2 ? '审核通过':'驳回'  ,
                                'name' => '店铺进件',
                                // 'store_name' => $storeInfo['store_name'],
                            ];
                            $withdraw->send($sendData);
                        }

                    }
                }


            } catch (\Exception $e) {
                return msg([], 1, "保存异常" . $e->getMessage());
            }
            return $save ? msg([], 0, "保存成功") : msg([], 1, "保存失败");
        }




    }
}